export function debounce(fn, delay) {
  let timer = null
  return function () {
      let args = arguments
      let context = this
      if (timer) {
          clearTimeout(timer)

          timer = setTimeout(function () {
              fn.apply(context, args)
          }, delay)
      } else {
          timer = setTimeout(function () {
              fn.apply(context, args)
          }, delay)
      }
  }
}

export function throttle(callback, delay) {
  var previousCall = new Date().getTime()
  return function () {
      var time = new Date().getTime()

      if ((time - previousCall) >= delay) {
          previousCall = time
          callback.apply(null, arguments)
      }
  }
}

export function sleep(timeout) {
  return new Promise(resolve => {
      setTimeout(() => {
          resolve()
      }, timeout);
  })
}

export function sleepv2(timeout) {
  let timer
  return {
      wait: new Promise(resolve => {
          timer = setTimeout(() => {
              resolve()
          }, timeout);
      }),
      timer
  }
}
